package quantum2.draw;

import quantum2.graph.StatResult;

import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.Locale;

final class ResultPanel extends JComponent {

    private static final int X = 10;

    private final NumberFormat df6;
    private final NumberFormat df4;
    private final DecimalFormat sdf;

    private StatResult result = null;

    public ResultPanel() {
        DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(Locale.US);
        df6 = new DecimalFormat("#.######", dfs);
        df4 = new DecimalFormat("#.####", dfs);
        sdf = new DecimalFormat("+0.#E0;-0.#E0", dfs);
        setBorder(BorderFactory.createCompoundBorder(
            BorderFactory.createEmptyBorder(5, 5, 5, 5),
            BorderFactory.createTitledBorder("")
        ));
    }

    public void setStat(StatResult result) {
        this.result = result;
        repaint();
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (result == null)
            return;
        double[] edgeEnergy = result.edgeEnergy;
        int x = getInsets().left + X;
        int y = getInsets().top + 20;
        FontMetrics fm = g.getFontMetrics();
        int h = fm.getHeight();
        g.drawString("Time: " + df4.format(result.currentTime), x, y);
        y += h;
        g.drawString("Energy", x + 50, y);
        y += h;
        int i = 0;
        for (double e : edgeEnergy) {
            //double len = result.g.getEdgeLength(i).doubleValue();
            g.drawString("Edge " + (i + 1) + ": " + df6.format(e), x, y);
            y += h;
            i++;
        }
        double de = result.totalEnergy - 1;
        g.drawString("Total energy: 1" + sdf.format(de), x, y);
    }

    public void clearStat() {
        result = null;
        repaint();
    }

    public Dimension getPreferredSize() {
        return new Dimension(200, 20);
    }
}
